package com.aier.classes.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.common.ClassesFinalPageSize;
import com.aier.classes.entity.ClassStudent;
import com.aier.classes.entity.InviteParent;
import com.aier.classes.entity.InviteTeacher;
import com.aier.classes.service.IClassUserService;
import com.aier.classes.service.IInviteRequestService;
import com.aier.common.dao.ICommonDAO;
import com.aier.school.entity.SchoolGrade;
import com.aier.school.service.ISchoolService;

@Scope("prototype")
@Component("classesMemberInviteAction")
public class ClassesMemberInviteAction extends ClassesBaseAction{

    /**
     * 班级管理-班级成员邀请
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "schoolService")
    private ISchoolService schoolService;    
    @Resource(name = "inviteRequestService")
    private IInviteRequestService inviteRequestService;    
    @Resource(name = "classUserService")
    private IClassUserService classUserService;    
    
    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息
    
    private List<SchoolGrade> schoolGradeList;        //职务列表
    private List<InviteTeacher> inviteTeacherList;    //教师邀请记录列表
    private List<InviteParent> inviteParentList;    //家长邀请记录列表
    private List<ClassStudent> classStudentList;    //班级学生列表
    private Long itid;            //教师邀请记录id
    private Long ipid;            //家长邀请记录id
    private Long cid;            //班级id
    private String name;        //姓名(学生,家长,教师)
    private String phone;        //手机号
    private String relation;    //家长和学生的关系
    private Long csid;            //班级学生id
    private Long gid;            //职务id
    private Long tid;            //发送邀请的教师tid
    private String invitePar_message;                //家长邀请的字符串 "csid,relation,pname,phone@XXX"
    private String inviteTea_message;                //教师邀请的字符串 "tname,gid,phone@XXX"
    
    
    private int page;            //页数
    private int page_size;        //每页的记录数
    private int page_count;        //总页数
    private int count;            //总数
    
//===================================================================================================        
    @Override
    public String execute(){
        Integer sid = schoolService.searchSchoolByCid(cid).getSid();
        schoolGradeList = schoolService.searchSchoolGradeList(sid);
        classStudentList = classUserService.searchClassStudentList(cid);
        return SUCCESS;
    }
    
    /**
     * 检查是否存在同名学生
     */
    public String checkClassStudent(){
        List<ClassStudent> csList = classUserService.searchClassStudentByName(name);
        if(csList!=null&&csList.size()>0){
            s = 0;//存在同名
        }else{
            s = 1;
        }
        return SUCCESS;
    }
    
    /**
     * 创建学生
     */
    public String addClassStudent(){
        s =0;
        Long csid = classUserService.insertClassStudent(cid, name,null);
        if (csid != 0) {
            s = 1;
        }
        return SUCCESS;
    }
    
//===================================================================================================    
    
    /**
     * 查询班级邀请家长记录
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchInviteParentList(){
        s = 1;
        Map<String,Object> map = inviteRequestService.searchInviteParentList(page, ClassesFinalPageSize.TEACHER_INVITE_LIST_PAGESIZE, cid);
        if(null==map){
            s = 0;
        }
        inviteParentList = (List<InviteParent>) map.get(ICommonDAO.OBJ_LIST);
        if (null == inviteParentList) {
            s = 0;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        return SUCCESS;
    }

    /**
     * 添加班级家长邀请
     * @return
     */
    public String addParentInvite(){
//        Timestamp ctime = new Timestamp(new Date().getTime());
//        InviteParent inviteParent = new InviteParent(null, name, phone, csid, relation, tid, cid, null, ctime);
        s = inviteRequestService.insertInviteParentList(tid, cid,invitePar_message);
        return SUCCESS;
    }
    
    /**
     * 删除家长邀请记录
     * @return
     */
    public String deleteParentInvite(){
        s = inviteRequestService.deleteInviteParent(ipid);
        return SUCCESS;
    }
    
//===================================================================================================
    /**
     * 查询班级邀请教师记录
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchInviteTeacherList(){
        s = 1;
        Integer sid = schoolService.searchSchoolByCid(cid).getSid();
        Map<String,Object> map = inviteRequestService.searchInviteTeacherList(page, ClassesFinalPageSize.TEACHER_INVITE_LIST_PAGESIZE, cid, sid);
        if(null==map){
            s = 0;
        }
        inviteTeacherList = (List<InviteTeacher>) map.get(ICommonDAO.OBJ_LIST);
        if (null == inviteTeacherList) {
            s = 0;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        return SUCCESS;
    }

    /**
     * 添加班级教师邀请
     * @return
     */
    public String addTeacherInvite(){
        Integer sid = schoolService.searchSchoolByCid(cid).getSid();
        s = inviteRequestService.insertInviteTeacherList(sid, cid,tid,inviteTea_message);
        return SUCCESS;
    }
    
    /**
     * 删除教师邀请记录
     * @return
     */
    public String deleteTeacherInvite(){
        s = inviteRequestService.deleteInviteTeacher(itid);
        return SUCCESS;
    }

//===================================================================================================

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public List<SchoolGrade> getSchoolGradeList() {
        return schoolGradeList;
    }

    public List<InviteTeacher> getInviteTeacherList() {
        return inviteTeacherList;
    }

    public List<InviteParent> getInviteParentList() {
        return inviteParentList;
    }

    public List<ClassStudent> getClassStudentList() {
        return classStudentList;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public void setInviteRequestService(IInviteRequestService inviteRequestService) {
        this.inviteRequestService = inviteRequestService;
    }

    public void setClassUserService(IClassUserService classUserService) {
        this.classUserService = classUserService;
    }

    public void setItid(Long itid) {
        this.itid = itid;
    }

    public void setIpid(Long ipid) {
        this.ipid = ipid;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setRelation(String relation) {
        this.relation = relation;
    }

    public void setCsid(Long csid) {
        this.csid = csid;
    }

    public void setGid(Long gid) {
        this.gid = gid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public Long getCid() {
        return cid;
    }

    public Long getTid() {
        return tid;
    }

    public void setInvitePar_message(String invitePar_message) {
        this.invitePar_message = invitePar_message;
    }

    public void setInviteTea_message(String inviteTea_message) {
        this.inviteTea_message = inviteTea_message;
    }

    
    
    
    
    
    
    
    
}